$(function(){
  // 功能介绍：
  // 将之前的旧的个人信息填上，只需要修改想要修改的信息，然后提交
  var userAcc = getCookie("userAccount");
  $(".userInfo .userName span").text('[ '+getCookie("userName")+" ]");
  $.ajax({
    type: "POST",
    url: "../php/user/userinfo_display.php",
    // data: {userName: userName, userEmail: userEmail, userTel: userTel}, //{name: value, name:value}
    dataType: "json",
    success: function (response) {
      console.log(response);
      
      $(".modifyContainer .userName").val(response[0].Uname);
      $(".modifyContainer .userEmail").val(response[0].Umail);
      $(".modifyContainer .userTel").val(response[0].Utel);
    },
    error : function(response){
      console.log("request fail");
      console.log(response);
    }
  });
  // 页面谈出动画效果
  $(".quoto").fadeOut(100);
  $(".quoto").fadeIn();
  $(".content").fadeOut(100);
  $(".content").fadeIn();

  // 修改提交按钮
  $(".content .submit")[0].onclick = function(){
    // 获取相关信息
    var userName = $(".modifyContainer .userName").val();
    var userEmail = $(".modifyContainer .userEmail").val();
    var userTel = $(".modifyContainer .userTel").val();
    if(userName == '' && userEmail == '' && userTel == ""){
      alert("请输入相关个人信息")
    }
    else if(userName == ''){
      alert("用户名不能为空！请重新输入：");
    }else if(userEmail == ''){
      alert("邮箱不能为空！请重新输入：");
    }else if(userTel == ''){
      alert("电话不能为空！ 请重新输入：")
    }else{
      if(confirm('确认更改吗？') == true){
        $.ajax({
          type: "POST",
          url: "../php/user/userinfo_change.php",
          data: {userName: userName, userEmail: userEmail, userTel: userTel}, //{name: value, name:value}
          dataType: "json",
          success: function (response) {
            console.log(response);
            if(response.success == 'update'){
              addCookie("userName", userName, 5, '\\');
              alert("修改成功！");
            }
            // 然后跳转到个人信息模块
            window.location.href = "../html/user-module-info.html";
          },
          error : function(response){
            console.log("request fail");
            console.log(response);
          }
        });
      }
      
    }
    
  }
})
